package com.cat.dataStructure;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/product-of-array-except-self/?envType=study-plan-v2&envId=top-100-liked
 * @create 2025/9/25 21:05
 * @since JDK17
 */

public class Solution79 {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] ans = new int[n], pre = new int[n + 1], suf = new int[n + 1];
        pre[0] = suf[n] = 1;
        for (int i = 0; i < n; i++) {
            pre[i + 1] = nums[i] * pre[i];
            suf[n - i - 1] = nums[n - i - 1] * suf[n - i];
        }
        for (int i = 0; i < n; i++) {
            ans[i] = pre[i] * suf[i + 1];
        }

        return ans;
    }
}
